package com.library.gq.servlet;


import com.library.gq.dao.imlp.BookDaoImpl;
import com.library.gq.entity.BookEntity;
import com.library.gq.servce.impl.BookServceImpl;
import com.library.gq.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BookServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("UTF-8");//获取代码格式
       resp.setContentType("text/html;charset=utf-8");//获取带吗格式

        BookServceImpl bsi=new BookServceImpl();//调用BookServletImpl的实体类
        BookDaoImpl bookDao=new BookDaoImpl();//调用BookDaoImpl的实体类
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||currentPage.equals("")){
            currentPage="1";
        }
        Integer currPage=Integer.parseInt(currentPage);
        //获取操作码
        String tp=req.getParameter("tp");
        if(tp==null){
            tp="sel";
        }
        if(tp.equals("sel")){
            String name=req.getParameter("select");
            if (name==null||name.equals("")){
                name="全部";
            }
            Page<BookEntity> page= bsi.findByPage(name,currPage);
            req.setAttribute("page",page);
            //转发
            req.getRequestDispatcher("gq/jsp/lostThings.jsp").forward(req,resp);
        }

        //增加
        if (tp.equals("insert")){
            String name=req.getParameter("name");
            String time= req.getParameter("time");
            String statr= req.getParameter("state");
            if (statr.equals("未领走")){
                statr="0";
            }else if(statr.equals("已领走")){
                statr="1";
            }
            int counts =Integer.parseInt(statr);
            Integer count= bookDao.Insert(name,time,counts);
            if (count==1){
                req.getRequestDispatcher("gq/jsp/lostThings.jsp").forward(req,resp);
            }
        }



        //增加领走信息
        if (tp.equals("insert_claim")){
                String id=req.getParameter("id");
            System.out.println(id);
                Integer ins=Integer.parseInt(id);
                String name_claim=req.getParameter("name_claim");
            System.out.println(name_claim);

            String card_claim=req.getParameter("card_claim");
            System.out.println(card_claim);

            String date_claim=req.getParameter("date_claim");
            System.out.println(date_claim);

            String phone_claim=req.getParameter("phone_claim");
            System.out.println(phone_claim);

            String stater=req.getParameter("state");
                Integer sum=0;
                if(stater.equals("已领走")){
                    sum=1;
                }

            /**
             * 根据id为条件进行修改
             */
            bookDao.update(sum,ins);
            Integer count=bookDao.Insert_claim(ins,name_claim,card_claim,date_claim,phone_claim);
            bookDao.update(sum,ins);
            if(count==1){
                    req.getRequestDispatcher("gq/jsp/lostThings.jsp").forward(req,resp);
                }

        }

        /**
         * 查询claim表的全部数据
         */
        if(tp.equals("claim_select")){
            Page<BookEntity> page= bsi.claim(currPage);
            req.setAttribute("page", page);
            req.getRequestDispatcher("gq/jsp/claimSelect.jsp").forward(req, resp);
            }

        }
        }



